<template>
  <div class="city-pay-bar" :id="id"></div>
</template>
<script>
  import echarts from 'echarts/lib/echarts'
  import 'echarts/lib/chart/bar'
  import 'echarts/lib/component/legend'
  import 'echarts/lib/component/tooltip'
  import { mapActions, mapState} from 'vuex'

  export default {
    props: {
      id: {
        type: String,
        required: true
      }
    },
    computed: {
      ...mapState(['cityMonthPay'])
    },
    watch: {
      cityMonthPay (val, oldval) {
        this.onReady()
      }
    },
    mounted () {
      this.onReady()
    },
    methods: {
      onReady () {
        const { id, cityMonthPay } = this
        const option = {
          title: {
            text: '1年走势',
            textStyle: {
              color: '#ffffff',
              fontWeight: 'normal',
              fontSize: 16
            },
            top: 0,
            left: '4%'
          },
          tooltip: {
            trigger: 'axis',
            position: function (pos, params, dom, rect, size) {
              // 鼠标在左侧时 tooltip 显示到右侧，鼠标在右侧时 tooltip 显示到左侧。
              var obj = {top: 60}
              obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 5
              return obj
            },
            axisPointer: { type: 'shadow' },
            formatter: function (params) {
              let name = params[0].name
              let name0 = params[0].seriesName
              let value0 = params[0].value
              let dom = `${name}<br/>${name0}:${value0}万笔`
              return dom
            }
          },
          grid: {
            left: '4%',
            right: '3%',
            top: '22%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            data: cityMonthPay.xAxis,
            axisLabel: {
              rotate: 35,
              textStyle: {
                color: '#52bfc9'
              }
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            z: 10
          },
          yAxis: {
            axisLabel: {
              textStyle: {
                color: '#52bfc9'
              },
              formatter: (value, index) => {
                return parseInt(value) + '万'
              }
            },
            min: value => {return value.min - 20},
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: 'solid',
                color: ['rgba(27, 29, 31, 1)']
              }
            }
          },
          dataZoom: [
            {
              type: 'inside',
              start: 0,
              end: 100,
            }
          ],
          series: [
            {
              name: '支付笔数',
              type: 'bar',
              barWidth: '40%',
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      {offset: 0, color: '#0bd4ff'},
                      {offset: 1, color: '#3b80ff'}
                    ]
                  ),
                  barBorderRadius: 20
                },
                emphasis: {
                }
              },
              data: cityMonthPay.data.map((item) => {
                return (parseInt(item) / 10000).toFixed(4)
              }),
              markLine: {
                label: {
                  normal: {
                    position: 'middle',
                    formatter: function (params) {
                      let dom = `${params.value}万笔`
                      return dom
                    }
                  }
                },
                lineStyle: {
                  normal: {
                    type: 'dashed',
                    width: 0.5,
                    color: '#0dffeb'
                  }
                },
                data: [
                  {
                    type: 'average'
                  }
                ]
              }
            }
          ],
          color: ['#0bd4ff','#3b80ff']
        }
        let bar = echarts.init(document.getElementById(id))
        bar.setOption(option, true)
        window.addEventListener('resize', function () {
          bar.resize()
        })
      }
    }
  }
</script>
<style type="text/css" scoped>
  .city-pay-bar{
    width: 100%;
    height: 39%;
  }
</style>
